<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>xeokit Example</title>
    <link href="../css/pageStyle.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
    <style>
        #sliderContainer {
            pointer-events: all;
            height: 100%;
            /*overflow-y: scroll;*/
            overflow-x: hidden;
            position: absolute;
            background-color: rgba(255, 255, 255, 0.2);
            color: black;
            top: 80px;
            z-index: 200000;
            float: left;
            left: 0;
            padding-left: 10px;
            font-family: 'Roboto', sans-serif;
            font-size: 15px;
            user-select: none;
            -ms-user-select: none;
            -moz-user-select: none;
            -webkit-user-select: none;
            width: 250px;
        }
        input {
            vertical-align: middle;
            padding:0px;
            margin:5px;
        }
    </style>
</head>
<body>
<input type="checkbox" id="info-button"/>
<label for="info-button" class="info-button"><i class="far fa-3x fa-question-circle"></i></label>
<canvas id="myCanvas"></canvas>
<div id="sliderContainer">
    <p>Sliders</p>
    <label>
        Explode factor
        <input id="explode_factor" type="range" min="0" max="2" value="0" step="0.05">
    </label>
</div>
<div class="slideout-sidebar">
    <img class="info-icon" src="../../assets/images/obj_logo.png"/>
    <h1>OBJLoaderPlugin</h1>
    <h2>Exploding an OBJ Model</h2>
    <h3>Components used</h3>
    <ul>
        <li>
            <a href="../../docs/class/src/viewer/Viewer.js~Viewer.html"
               target="_other">Viewer</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/OBJLoaderPlugin/OBJLoaderPlugin.js~OBJLoaderPlugin.html"
               target="_other">OBJLoaderPlugin</a>
        </li>
    </ul>
</div>
</body>
<script type="module">

    //------------------------------------------------------------------------------------------------------------------
    // Import the modules we need for this example
    //------------------------------------------------------------------------------------------------------------------

    import {Viewer, OBJLoaderPlugin} from "../../dist/xeokit-sdk.min.es.js";

    //------------------------------------------------------------------------------------------------------------------
    // Create a Viewer and arrange camera
    //------------------------------------------------------------------------------------------------------------------

    const viewer = new Viewer({
        canvasId: "myCanvas",
        transparent: true
    });

    viewer.camera.orbitPitch(20);

    //------------------------------------------------------------------------------------------------------------------
    // Load OBJ model and fit it to view, then start orbiting the camera
    //------------------------------------------------------------------------------------------------------------------

    const objLoader = new OBJLoaderPlugin(viewer);

    const sceneModel = objLoader.load({
        id: "myModel",
        src: "../../assets/models/obj/sportsCar/sportsCar.obj",
        edges: true
    });

    //------------------------------------------------------------------------------------------------------------------
    // Explodes a model
    //------------------------------------------------------------------------------------------------------------------

    let explodeFactor = 0.0;

    function getCenter(aabb){
        return [
            (aabb[0] + aabb[3]) / 2,
            (aabb[1] + aabb[4]) / 2,
            (aabb[2] + aabb[5]) / 2,
        ];
    }

    let modelCenter = getCenter(sceneModel.aabb);

    function moveAwayFromModelCenter(child){
        let childCenter = getCenter(child.aabb);
        let vectorFromCenter = [
            childCenter[0] - modelCenter[0],
            childCenter[1] - modelCenter[1],
            childCenter[2] - modelCenter[2]
        ];
        child.position = [
            vectorFromCenter[0] * explodeFactor,
            vectorFromCenter[1] * explodeFactor,
            vectorFromCenter[2] * explodeFactor,
        ];
    }

    function resetExplode(){
        explodeFactor = 0.0;
        sceneModel.children.forEach(moveAwayFromModelCenter);
    }

    sceneModel.on("loaded", () => {
        viewer.cameraFlight.jumpTo(sceneModel);
    });

    document.getElementById("explode_factor").oninput = function() {
        resetExplode();
        explodeFactor = Number(document.getElementById("explode_factor").value);
        sceneModel.children.forEach(moveAwayFromModelCenter);
    };

</script>
</html>